package by.kniazhych.core.server.local {
	
	/**
	 * ...
	 * @author Boutylin Mikhail
	 */
	
	import by.kniazhych.core.server.local.ai.AI;
	import by.kniazhych.core.server.local.core.Motion;
	import by.kniazhych.core.server.local.data.BoardData;
	import by.kniazhych.core.constants.PeaceType;
	import by.kniazhych.core.constants.UserColor;
	import by.kniazhych.core.data.game.PeacePosition;
	import by.kniazhych.core.server.common.BaseServerCommandDispatcher;
	import by.kniazhych.core.server.common.ClientCommands;
	import by.kniazhych.core.server.common.IServerProxy;
	import by.kniazhych.core.server.common.ServerActionCommand;
	import by.kniazhych.core.server.common.ServerActionFlag;
	import by.kniazhych.core.server.common.ServerCommands;
	import by.kniazhych.core.server.common.UserNotifycation;
	import by.kniazhych.core.server.local.events.ServerGameEvent;
	import by.kniazhych.core.server.local.logic.LogicController;
	import by.kniazhych.core.server.local.rules.GameRules;
	import by.kniazhych.core.server.local.rules.IPlayer;
	import by.vaukalak.core.commands.Command;
	import by.vaukalak.core.commands.CommandDispatcher;
	import flash.events.Event;
		
	public class LocalServer extends BaseServerCommandDispatcher{
		
		private var _proxy:IServerProxy;
		private var _boardData:BoardData;
		private var _rules:GameRules;
		private var _enabled:Boolean = false;
		private var _player:LocalPlayer;
		
		public function LocalServer(proxy:IServerProxy) {
			super();
			_proxy = proxy;
			
			
		}
		
		
		
		//--------------------------------------------------------------------------
		//
		//  PUBLIC METHODS
		//
		//--------------------------------------------------------------------------
		
		public function connect(s:String, color:uint):void {
			
			_boardData = new BoardData();
			var board:Array = new Array(_boardData.board.length);
			for (var i:int = 0; i < _boardData.board.length; i++) {
				board[i] = [];
				(board[i] as Array).push.apply(null, _boardData.board[i]);
			}
			super.dispatchServerCommand(ServerCommands.BUILD_GAME, ServerActionFlag.UNIQUE_COMMAND, [board, color]);
			_boardData.addEventListener(ServerGameEvent.PLAYER_CHANGED, _onPlayerChanged);
			_boardData.addEventListener(ServerGameEvent.MOTION_PROCESSED, _onMotionProcessed);
			_boardData.addEventListener(ServerGameEvent.PEACE_CHANGED, _onPeaceChanged);
			_boardData.addEventListener(ServerGameEvent.GAME_OVER, _handleGameOver);
			
			_player = new LocalPlayer(_boardData, color);
			_player.addEventListener(ServerGameEvent.PEACE_CHOISE, _onPeaceChoise);
			_rules = new GameRules(_boardData, _player);
			
		}
		
		public function motionRequest(sP:PeacePosition, fP:PeacePosition):void {
			if (!_player.processMotion(sP, fP)) {
				super.dispatchServerCommand(ServerCommands.PROCESS_MOTION, ServerActionFlag.UNIQUE_COMMAND, [sP, sP]);
			}
		}
		
		public function selectPeace(type:uint):void {
			_player.selectPeace(type);
		}
		
		//--------------------------------------------------------------------------
		//
		//  PRIVATE METHODS
		//
		//--------------------------------------------------------------------------
		
		
		private function _dispatchTurnChangeCommands():void {
			if (_boardData.gameOver) {
				super.dispatchServerCommand(ServerCommands.NOTIFY_USER, ServerActionFlag.UNIQUE_COMMAND, [UserNotifycation.VICTORY]);
				return;
			}
			if (_boardData.tron) {
				super.dispatchServerCommand(ServerCommands.NOTIFY_USER, ServerActionFlag.UNIQUE_COMMAND, [UserNotifycation.TRON]);
			}
			if (_boardData.rokash) {
				super.dispatchServerCommand(ServerCommands.NOTIFY_USER, ServerActionFlag.UNIQUE_COMMAND, [UserNotifycation.ROKASH]);
			}else if (_boardData.shah) {
				super.dispatchServerCommand(ServerCommands.NOTIFY_USER, ServerActionFlag.UNIQUE_COMMAND, [UserNotifycation.SHAH]);
			}
			
		}
		
		//--------------------------------------------------------------------------
		//
		//  EVENT HANDLER
		//
		//--------------------------------------------------------------------------
		
		private function _onPeaceChoise(e:ServerGameEvent):void {
			super.dispatchServerCommand(ServerCommands.DISPLAY_PEACE_CHOISE, ServerActionFlag.UNIQUE_COMMAND, [_player.missingPeaces]);
		}
		
		private function _onMotionProcessed(e:ServerGameEvent):void {
			//trace(_boardData.lastMotion);
			super.dispatchServerCommand(ServerCommands.PROCESS_MOTION, ServerActionFlag.UNIQUE_COMMAND, [_boardData.lastMotion.sP, _boardData.lastMotion.fP]);
			
		}
		
		
		private function _handleGameOver(e:ServerGameEvent):void {
			super.dispatchServerCommand(ServerCommands.NOTIFY_USER, ServerActionFlag.UNIQUE_COMMAND, [UserNotifycation.VICTORY]);
		}
		
		private function _onPeaceChanged(e:ServerGameEvent):void {
			_dispatchTurnChangeCommands();
			super.dispatchServerCommand(ServerCommands.CHANGE_PEACE, ServerActionFlag.UNIQUE_COMMAND, [_boardData.lastChangedPeacePosition, _boardData.lastChangedPeaceType]);
			if (_boardData.lastChangedPeaceType == PeaceType.WHITE_KNIAZ || _boardData.lastChangedPeaceType == PeaceType.BLACK_KNIAZ)
				super.dispatchServerCommand(ServerCommands.NOTIFY_USER, ServerActionFlag.UNIQUE_COMMAND, [UserNotifycation.DECLARE_KNIAZ]);
			super.dispatchServerCommand(ServerCommands.GIVE_TURN, ServerActionFlag.UNIQUE_COMMAND, [_boardData.currentPlayer.color]);
		}
		
		private function _onPlayerChanged(e:ServerGameEvent):void {
			_dispatchTurnChangeCommands();
			super.dispatchServerCommand(ServerCommands.GIVE_TURN, ServerActionFlag.UNIQUE_COMMAND, [_boardData.currentPlayer.color]);
		}
		
	}

}